using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;
using VM.Common.DataUntil;
using VM.Common.http;
using VM.Entity.EntityDto;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.MsgCode;
using VM.IBLL;
using VM.IBLL.Member;
using VM.IBLL.Msg;
using VM.IBLL.Token;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;


namespace WeatherForecastTest.Controllers
{


    /// <summary>
    ///验证码
    /// </summary>
    [ApiController]
    [Route("api/msg")]
    public class MSGController : ControllerBase
    {
 
        private readonly IMemberBLL _IUserBLL;
        private readonly IMsgBLL _IMsgBLL;

        public MSGController(IJWTService iJWTService,  IMemberBLL iUserBLL,IMsgBLL iMsgBLL)
        {
            
            _IUserBLL = iUserBLL;
            _IMsgBLL = iMsgBLL;

        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="_msgCode">登录信息</param>
        /// <returns></returns>
        [HttpPost]
        //[Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("sendMsgCode")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public JsonObject sendMsgCode(MsgCode _msgCode)
        { 
            HttpClientRequest hc = new HttpClientRequest();
            string JsonData= JsonConvert.SerializeObject(_msgCode);
            var  Res=hc.AsyncRequestSendMsg(_msgCode.orgid, _msgCode.phone);
            JObject Jo = (JObject)JsonConvert.DeserializeObject(Res);
            if (Convert.ToInt32(Jo["status"]) == 200)
            {
                //保存到数据库中将信息
                sys_message _ms_Code = new sys_message();
                _ms_Code.phone = Jo["data"]["phone"].ToString();
                _ms_Code.code = Jo["data"]["sms"].ToString();
                _ms_Code.createDate = Timestamp.getNowTimeStamp();
                _ms_Code.endDate = _ms_Code.createDate+300;
               
                _IMsgBLL.AddMsg(_ms_Code);
            }
            return (JsonObject)JsonObject.Parse(Res);
        }


      
         
        /// <summary>
        ///  校验验证码
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        //[Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("validMsgCode")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public JsonResult validMsgCode(ValidMsgCode obj)
        {
            //校验验证码是否可用，如果可用，返回true
         bool IsAbleUse=  _IMsgBLL.CheckMsgIsValid(obj.phone, obj.code);
            if (IsAbleUse)
            {
                return new JsonResult(
                      new
                      {
                          status = 200,
                          msg = "验证码可用",
                      }
                      ); 
            }
            else
            {
                return new JsonResult(
                        new
                        {
                            status = 401,
                            msg = "验证码不可用",
                        }
                        );
            }
        }
    }
}